import {legacy_createStore as createStore, applyMiddleware} from 'redux';
import reducers from '.';
import createSagaMiddleware from 'redux-saga';
import rootSaga from './saga/index';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
//创建一个持久化配置
//sharedp
//定义一个初始化的state
const persistConfig = {
  key: 'root', // 存储键名
  storage: AsyncStorage,
  whitelist: ['MainStore'],
  //如果变量不需要持久化储存，就可以从whitelist当中移除
};

const sagaMiddleware = createSagaMiddleware();
//将 Redux 的 reducer 持久化到本地存储中
const rootReducers = persistReducer(persistConfig, reducers);
//创建store,就收reducer对象，返回刚建好的store对象
const store = createStore(rootReducers, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(rootSaga);

const persistor = persistStore(store);

// 使用 export 关键字导出 persistor 和 store
export {store, persistor};
